#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){	
	// register touch events
	ofRegisterTouchEvents(this);
	
	// initialize the accelerometer
	ofxAccelerometer.setup();
	
	//iPhoneAlerts will be sent to this.
	ofxiPhoneAlerts.addListener(this);
	
	//If you want a landscape oreintation 
	//iPhoneSetOrientation(OFXIPHONE_ORIENTATION_LANDSCAPE_RIGHT);
	
	ofBackground(255,255,255);
	ofSetFrameRate(10);
	//ofSetBackgroundAuto(false);
	
	
    treat = false;
    size = 4;
	score = 0;
    
    xpos = ofGetWidth()/2;
    ypos = ofGetHeight()/2;
	
	j=0;
    xmov = 0;
    ymov = -1;
	
	
}

//--------------------------------------------------------------
void testApp::update(){
	//ofScale(0.5, 0.5, 1);

	
	//MAKE NEW TREAT
	if(treat == false){
		
        x1 = ((int)ofRandom(0,ofGetWidth()/20))*20;
        y1 = ((int)ofRandom(0,ofGetHeight()/20))*20;
		//ofSetColor(0,0,0);
		//ofDrawBitmapString(x1 + " " + y1, 10,10);
		//treatmaker(x1, y1);
        treat = true;
		
    }
	
	//IF TREAT IS EATEN INCREASE SCORE AND SIZE OF SNAKE ARRAY
	if(x1==xpos && y1==ypos) {
        treat = false;
		// x2 = x1;
        //y2 = y1;
		score += 10;
		size++; //increase size of snake array everytime treat is eaten
    }
	
	
	//CHANGE DIRECTION
	if (xmov==1){
        xpos+=20;
    }
    if (xmov==-1){
        xpos-=20;
    }
    if (ymov==1){
        ypos+=20;
    }
    if (ymov==-1){
        ypos-=20;
    }
	
	
	//SNAKE SIZE ARRAY
	for(i=0;i<size;i++){
		balls[i].x=balls[i+1].x;
		balls[i].y=balls[i+1].y;
	}
	
	//ADD CURRENT POSITION TO ARRAY
	balls[size-1].x = xpos;
	balls[size-1].y = ypos;
	
	
	//IF SNAKE GOES OUT OF BOUNDS
    if(xpos>(ofGetWidth()-20)) xpos = 0;
    if(xpos<0) xpos = ofGetWidth();
    if(ypos>(ofGetHeight()-20)) ypos = 0;
    if(ypos<0) ypos = ofGetHeight();
	
	//j = j+15;
	
	
}

//--------------------------------------------------------------
void testApp::draw(){
	//ofScale(0.5, 0.5, 1);
	
	for (i=0; i<size; i++) {
		
		ofSetColor(0,0,0);
		ofNoFill();
		ofRect(balls[i].x,balls[i].y,20,20);
    }
	
	
    ofSetColor(255,0,0);
    ofFill();
    ofRect(x1,y1,20,20);
		
	 
	 //ofSetColor(0,0,0);
	 //ofDrawBitmapString(ofToString(ofGetHeight()) + " " + ofToString(ofGetWidth()), ofGetWidth()/2, ofGetHeight()/2);
	 //ofDrawBitmapString(ofToString(balls[1].y), 64,j);
	 
	/*
    ofSetColor(0,0,0);
	ofDrawBitmapString("SCORE: "+ofToString(score), 560,10);
	*/
	
	
}

//--------------------------------------------------------------
void testApp::exit(){

}

//--------------------------------------------------------------
void testApp::touchDown(ofTouchEventArgs &touch){
	//ofSetColor(0,0,0);
	//ofDrawBitmapString("hello world",10,10);
	
	if (touch.y<=(ofGetHeight()*2/3)&&touch.y>ofGetHeight()/3&&touch.x<=ofGetWidth()/2) {
            xmov = -1;
            ymov = 0;
	}        
	else if (touch.y<=(ofGetHeight()*2/3)&&touch.y>ofGetHeight()/3&&touch.x>ofGetWidth()/2) {
            xmov = 1;
            ymov = 0;
	}
	else if (touch.y<=(ofGetHeight()/3)) {
            xmov = 0;
            ymov = -1;
	}
	else if (touch.y>(ofGetHeight()*2/3)) {
            xmov = 0;
            ymov = 1;
			
    }
	
}

//--------------------------------------------------------------
void testApp::touchMoved(ofTouchEventArgs &touch){

}

//--------------------------------------------------------------
void testApp::touchUp(ofTouchEventArgs &touch){

}

//--------------------------------------------------------------
void testApp::touchDoubleTap(ofTouchEventArgs &touch){

}

//--------------------------------------------------------------
void testApp::lostFocus(){

}

//--------------------------------------------------------------
void testApp::gotFocus(){

}

//--------------------------------------------------------------
void testApp::gotMemoryWarning(){

}

//--------------------------------------------------------------
void testApp::deviceOrientationChanged(int newOrientation){

}


//--------------------------------------------------------------
void testApp::touchCancelled(ofTouchEventArgs& args){

}

